import MySQLdb

########################################################################
class Data_Access (object):
    #----------------------------------------------------------------------
    def __init__ (self, id_vulnerability, pl_attack):
        """
        Construtor:
        Cria conexão com a Base de Dados
        """        
        try:	# Ainda falta tratar erros... 
            db = MySQLdb.connect (
                host = 'localhost',
                user = 'root',
                passwd = '',
                db = 'DB_1CLIC2LEARN'
                )      
            self.id_vulnerability = id_vulnerability
            self.pl_attack = pl_attack
        except _mysql_exceptions.OperationalError as e:
            print e
            return [("Can't connect to local MySQL server"), (e)]
    
    
    #----------------------------------------------------------------------
    def get_Attack (self):
        """
        Retorna os ataques
        """        
        self.cursor = db.cursor()
        self.cursor.execute('SELECT ATTACK FROM ' + pl_attack)
        self.result = cursor.fetchall()
        
        if result:
            for z in result:
                print z
        
        return result
    
    
    #----------------------------------------------------------------------
    def get_Mitigation (self):
        """
        Retorna formas de mitigar a vulnerabilidade identificada nos testes
        """
        self.cursor = db.cursor()
        self.cursor.execute('SELECT MITIGATION FROM ' + id_vulnerability)
        self.result = cursor.fetchall()
        
        if result:
            for z in result:
                print z
        
        return result

    #----------------------------------------------------------------------
    def get_Response (self):
        """
        Retorna as respostas que a vulnerabilidade informada apresenta
        """
        self.cursor = db.cursor()
        self.cursor.execute('SELECT ATTACK_RESPONSE FROM ' + id_vulnerability)
        self.result = cursor.fetchall()
        
        if result:
            for z in result:
                print z
        
        return result
    